<?php
/**
 * 
 * 更新用户教育信息
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","cron_update_user_education");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;


//15分钟一次,执行1000条
define("PCOUNT",20);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());


include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");

class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		$sql = "select count(*) as count from user_info where school_id = 0";
		
		$db_base = new db_base();
		$count = $db_base->select($sql);
		$count = $count[0]["count"];
		echo "tag_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		
	}	
	
	function get_list_by_page($p)
	{
		$start = ($p-1)*PCOUNT;
		
		$sql = "select uid from user_info where school_id=0 limit  $start," .PCOUNT;
		
		$db_base = new db_base();
		$list = $db_base->select($sql);
		
		$uid_array = array();
		foreach($list as $value)
		{
			$uid_array[] = $value["uid"];
			
		}
		
		$mblog_from_api = new mblog_from_api();
		$user_list = $mblog_from_api->api_get_education_by_uids(join(",",$uid_array));
		
		$education_array = array();
		foreach($user_list["data"] as $key => $value)
		{
			$education_array["{$value["id"]}"] = $value["education"];
		}
		
		
		foreach($list as $key => $value)
		{
			if($education_array[$value["uid"]])
			{
				
				$max_info = array();
				$max_year =0;
				foreach($education_array[$value["uid"]] as $key1 => $value1)
				{
					if($value["year"] >=  $max_year)
					{
						$max_year = $value1["year"];
						$max_info = $value1;
					}
				}
				
				$school_id = $max_info["school_id"];
				$school_name = $max_info["school"];
				$start_year = $max_info["year"];
				
				$sql ="update user_info set school_id=$school_id, school_name = '{$school_name}', start_year ='{$start_year}' where uid = {$value["uid"]}";
				$db_base->query($sql);
				$sql ="update user_search set school_id=$school_id, school_name = '{$school_name}', start_year ='{$start_year}' where uid = {$value["uid"]}";
				$db_base->query($sql);
			}
			else
			{
				$sql ="update user_info set school_id = -1 where uid = {$value["uid"]}";
				$db_base->query($sql);
				$sql ="update user_search set school_id = -1 where uid = {$value["uid"]}";
				$db_base->query($sql);
				
			}	

		}
		
	}
	
	
	
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
